package com.achao.view;


import com.achao.dao.IdentificationDao;
import com.achao.dao.PassengerDao;
import com.achao.javabean.entity.PageInfo;
import com.achao.javabean.qo.PassengerQo;
import com.achao.javabean.vo.PassengerVo;
import com.achao.utils.MybatisUtils;
import com.achao.utils.ParamUtils;
import com.achao.utils.StringUtils;
import lombok.Data;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;


@Data
public class UserManageView extends JFrame {

    private static UserManageView instance;

    public static UserManageView getInstance() {
        if (instance == null)
            instance = new UserManageView();
        return instance;
    }

    private DefaultTableModel model;
    private JLabel lPageText;
    private JTextField nameText;    //名字
    private JComboBox<String> identification;//证件类型
    private JTextField startTimeText;    //开始时间
    private JTextField endTimeText;        //结束时间


    private JButton btnSearch;
    private JButton btnNext;
    private JButton btnPrev;

    private PageInfo page;
    private PassengerQo passengerQo = new PassengerQo();

    private JButton FirstBtn = new JButton("<<");
    private JButton PrevBtn = new JButton("<");
    private JButton oneBtn = new JButton("1");
    private JButton twoBtn = new JButton("2");
    private JButton threeBtn = new JButton("3");
    private JButton fourBtn = new JButton("4");
    private JButton fiveBtn = new JButton("5");
    private JButton NextBtn = new JButton(">");
    private JButton LastBtn = new JButton(">>");
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    PassengerDao passengerDao = sqlSession.getMapper(PassengerDao.class);
    IdentificationDao identificationDao = sqlSession.getMapper(IdentificationDao.class);


    private UserManageView() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("用户管理");
        this.setResizable(false);
        this.setBounds(100, 100, 630, 500);
        this.setLocationRelativeTo(null);
        this.setLayout(null);

        renderSearch();
        renderTable();
        getData();
        renderPage();
        renderPageInfo();
//        sqlSession.close();
    }

    private void renderTable() {
        model = new DefaultTableModel();
        JTable table = new JTable(model);
        JScrollPane scrollPanel = new JScrollPane(table);
        scrollPanel.setBounds(10, 90, 600, 300);
        model.setColumnIdentifiers(new String[]{"用户名", "姓名", "手机号码", "证件类型",
                "证件号码", "旅客类型", "注册时间", "核验状态"});
        this.add(scrollPanel);

    }

    //渲染分页
    private void renderPage() {
        JPanel p = new JPanel();
        //上一页按钮
        btnPrev = new JButton("上一页");
        btnPrev.addActionListener(e -> {
            doPrev();
        });
        lPageText = new JLabel((page.getNowPage() + 1) + "/" + page.getSumPage());
        //下一页按钮
        btnNext = new JButton("下一页");
        btnNext.addActionListener(e -> {
            doNext();
        });
        btnPrev.setEnabled(false);
        btnNext.setEnabled(false);
        p.add(btnPrev);
        p.add(lPageText);
        p.add(btnNext);
        p.setBounds(20, 430, 200, 40);
        this.add(p);
        if (page.getNowPage() > 0) {
            btnPrev.setEnabled(true);
        }
        if (page.getSumPage() > 1 && page.getNowPage() != page.getSumPage()) {
            btnNext.setEnabled(true);
        }
    }

    // 高级分页渲染
    private void renderPageInfo() {
        JPanel p = new JPanel();
        p.add(FirstBtn);
        p.add(PrevBtn);
        p.add(oneBtn);
        p.add(twoBtn);
        p.add(threeBtn);
        p.add(fourBtn);
        p.add(fiveBtn);
        p.add(NextBtn);
        p.add(LastBtn);
        p.setBounds(180, 430, 500, 30);
        this.add(p);
        showBtn();
    }

    //渲染搜索框
    private void renderSearch() {
        JPanel p = new JPanel();
        p.setBounds(10, 10, 600, 80);
        p.setLayout(new FlowLayout());
        JLabel l = new JLabel("姓名");
        nameText = new JTextField(10);
        p.add(l);
        p.add(nameText);
        p.add(new JLabel("证件类型"));


        identification = new JComboBox<>();
        //加入获取证件类型的代码
        List<String> all = identificationDao.getAll();
        for (String s : all) {
            identification.addItem(s);
        }

        p.add(identification);

        JPanel p2 = new JPanel();
        p.add(new JLabel("注册时间"));

        startTimeText = new JTextField(5);
        endTimeText = new JTextField(5);
        p.add(startTimeText);
        p.add(new JLabel("-"));
        p.add(endTimeText);

        btnSearch = new JButton("查询");
        btnSearch.addActionListener((e) -> {
            doSearch();
        });
        p2.add(btnSearch);
        p.add(p2);
        this.add(p);
    }

    //从数据库获取数据并打印到表格
    private void getData() {
        System.out.println("获取数据并打印数据到表格");
        int total = passengerDao.getTotalByPQo(passengerQo);
        page = new PageInfo(total, ParamUtils.NOW_PAGE, ParamUtils.PAGE_SIZE);
        passengerQo.setNowPage((page.getNowPage() * page.getPageSize()));
        passengerQo.setPageSize(page.getPageSize());
        List<PassengerVo> viewData = passengerDao.getViewData(passengerQo);
        showManage(viewData);

    }


    private void doSearch() {
        System.out.println("点击了搜索");
        String name = nameText.getText();
        String idType = (String) identification.getSelectedItem();
        String startTime = startTimeText.getText();
        String endTime = endTimeText.getText();
        passengerQo.setNowPage(ParamUtils.NOW_PAGE * ParamUtils.PAGE_SIZE);
        passengerQo.setPageSize(ParamUtils.PAGE_SIZE);
        if (StringUtils.isNotEmpty(name)) {
            passengerQo.setPassengerName(name);
        } else {
            passengerQo.setPassengerName(null);
        }
        if (StringUtils.isNotEmpty(idType)) {
            passengerQo.setIdentificationType(idType);
        } else {
            passengerQo.setIdentificationType(null);
        }
        if (StringUtils.isNotEmpty(startTime)) {
            passengerQo.setStartTime(startTime);
        } else {
            passengerQo.setStartTime(null);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            passengerQo.setEndTime(endTime);
        } else {
            passengerQo.setEndTime(null);
        }

        List<PassengerVo> viewData = passengerDao.getViewData(passengerQo);
        showManage(viewData);

        int total = passengerDao.getTotalByPQo(passengerQo);
        page.setMyPage(total, ParamUtils.NOW_PAGE, ParamUtils.PAGE_SIZE);
        setPageLabel();

    }

    private void doPrev() {
        System.out.println("点击了上一页");
        int total = passengerDao.getTotalByPQo(passengerQo);
        page.setMyPage(total, page.getNowPage() - 1, page.getPageSize());
        passengerQo.setNowPage((page.getNowPage() * page.getPageSize()));
        setPageLabel();
        List<PassengerVo> viewData = passengerDao.getViewData(passengerQo);
        showManage(viewData);
    }

    private void doNext() {
        System.out.println("点击了下一页");
        int total = passengerDao.getTotalByPQo(passengerQo);
        page.setMyPage(total, page.getNowPage() + 1, page.getPageSize());
        passengerQo.setNowPage((page.getNowPage() * page.getPageSize()));
        setPageLabel();
        List<PassengerVo> viewData = passengerDao.getViewData(passengerQo);
        showManage(viewData);
    }

    private void showManage(List<PassengerVo> list) {
        model.setRowCount(0);
        for (PassengerVo passengerVo : list) {
            Object[] data = {passengerVo.getUserName(), passengerVo.getPassengerName(), passengerVo.getPhone(),
                    passengerVo.getIdentificationType(), passengerVo.getIdentificationNum(), passengerVo.getTravellerType(),
                    passengerVo.getCreateTime(), (passengerVo.getStatus() == 0 ? "未核验" : "已核验")};
            model.addRow(data);
        }
    }

    private void setPageLabel() {
        lPageText.setText((page.getNowPage() + 1) + "/" + page.getSumPage());
        if (page.getNowPage() > 0) {
            btnPrev.setEnabled(true);
        } else {
            btnPrev.setEnabled(false);
        }
        if (page.getSumPage() > 1 && (page.getNowPage() + 1) != page.getSumPage()) {
            btnNext.setEnabled(true);
        } else {
            btnNext.setEnabled(false);
        }
    }

    private void showBtn() {
        if (Integer.parseInt(fiveBtn.getText()) < page.getSumPage()) {
            NextBtn.setEnabled(true);
            LastBtn.setEnabled(true);
        }else{
            NextBtn.setEnabled(false);
            LastBtn.setEnabled(false);
        }
        if (Integer.parseInt(oneBtn.getText()) == 1){
            FirstBtn.setEnabled(false);
            PrevBtn.setEnabled(false);
        }else{
            FirstBtn.setEnabled(true);
            PrevBtn.setEnabled(true);
        }
        if (page.getSumPage() > 4) {
            oneBtn.setEnabled(true);
            twoBtn.setEnabled(true);
            threeBtn.setEnabled(true);
            fourBtn.setEnabled(true);
            fiveBtn.setEnabled(true);
        } else {
            if (page.getSumPage() == 1) {
                oneBtn.setEnabled(true);
            } else {
                twoBtn.setEnabled(false);
                threeBtn.setEnabled(false);
                fourBtn.setEnabled(false);
                fiveBtn.setEnabled(false);
            }
            if (page.getSumPage() == 2) {
                oneBtn.setVisible(true);
                twoBtn.setVisible(true);
            } else {
                threeBtn.setEnabled(false);
                fourBtn.setEnabled(false);
                fiveBtn.setEnabled(false);
            }
            if (page.getSumPage() == 3) {
                oneBtn.setVisible(true);
                twoBtn.setVisible(true);
                threeBtn.setVisible(true);
            } else {
                fourBtn.setEnabled(false);
                fiveBtn.setEnabled(false);
            }
            if (page.getSumPage() == 4) {
                oneBtn.setVisible(true);
                twoBtn.setVisible(true);
                threeBtn.setVisible(true);
                fourBtn.setEnabled(true);
            } else {
                fiveBtn.setEnabled(false);
            }

        }
    }
}
